//
//  CNShareView.m
//  CNShareView
//
//  Created by 孟瑞瑞 on 2018/7/15.
//  Copyright © 2018年 CSDN. All rights reserved.
//

#import "CNShareView.h"
#import "CNShareItem.h"
#import "CNCustomButton.h"
#import <CNBaseTool/CNCategoryHeader.h>

@interface CNShareView ()
@property(nonatomic,strong)UIButton *bgView;
@property(nonatomic,strong)UIView *contentView;
@property(nonatomic,strong)UIScrollView *shareScrollView1;
@property(nonatomic,strong)UIView *splitLine1;
@property(nonatomic,strong)UIScrollView *shareScrollView2;
@property(nonatomic,strong)UIView *splitLine2;
@property(nonatomic,strong)UIButton *cancelBtn;

@property(nonatomic,assign)CGFloat scrollViewHeight;
@property(nonatomic,assign)CGFloat cancelBtnHeight;
@property(nonatomic,assign)CGFloat contentViewHeight;
@property(nonatomic,assign)CGSize shareBtnSize;
@property(nonatomic,assign)CGFloat shareBtnSpace;


@end

@implementation CNShareView

- (id)init{
    self = [super init];
    if(self){
        self.scrollViewHeight = 72;
        self.cancelBtnHeight = 44;
        if (iPhoneX)
        {
            self.contentViewHeight = 2 * self.scrollViewHeight + 24*4 + self.cancelBtnHeight + 16 + 34 ;
        }
        else
        {
            self.contentViewHeight = 2 * self.scrollViewHeight + 24*4 + self.cancelBtnHeight + 16 ;
        }
        
        self.shareBtnSize = CGSizeMake(54, 72);
        self.shareBtnSpace = 20 ;
        [self setupUI];
    }
    return self ;
}

#pragma mark -- 设置UI

- (void)setupUI{
    [self addSubview:self.bgView];
    [self addSubview:self.contentView];
    [self.contentView addSubview:self.splitLine1];
    [self.contentView addSubview:self.shareScrollView1];
    [self.contentView addSubview:self.splitLine2];
    [self.contentView addSubview:self.shareScrollView2];
    [self.contentView addSubview:self.cancelBtn];
    
    [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    
    self.contentView.frame = CGRectMake(0, [[UIScreen mainScreen]bounds].size.height, [[UIScreen mainScreen]bounds].size.width, self.contentViewHeight);
    
    UILabel *tipLabel = [[UILabel alloc] init];
    tipLabel.font = [UIFont systemFontOfSize:13];
    tipLabel.theme_textColor = shareLabelTextColorPicker;
    [self.contentView addSubview:tipLabel];
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(24);
        make.height.mas_equalTo(16);
        
    }];
    tipLabel.text = @"分享到：";
    
    self.syncBlinBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.syncBlinBtn.titleLabel.font = [UIFont systemFontOfSize:13];

    self.syncBlinBtn.selected = NO;
    [self.syncBlinBtn theme_setTitleColor:shareLabelTextColorPicker forState:(UIControlStateNormal)];
    [self.syncBlinBtn theme_setTitleColor:shareLabelTextColorPicker forState:(UIControlStateHighlighted)];
    [self.syncBlinBtn setTitle:@"同步到Blink动态" forState:(UIControlStateNormal)];
    [self.syncBlinBtn theme_setImage:syncBlinImage forState:(UIControlStateNormal)];
    [self.syncBlinBtn theme_setImage:syncBlinImage forState:(UIControlStateHighlighted)];
    [self.syncBlinBtn theme_setImage:syncBlinSelectImage forState:(UIControlStateSelected)];
    [self.syncBlinBtn theme_setImage:syncBlinImage forState:(UIControlStateDisabled)];
    [self.syncBlinBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 8, 0, 0)];
    [self.contentView addSubview:self.syncBlinBtn];
    [self.syncBlinBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-24);
        make.centerY.mas_equalTo(tipLabel);
        make.width.mas_equalTo(125);
    }];
    @weakify(self);
    [self.syncBlinBtn bk_addEventHandler:^(UIButton *sender) {
        @strongify(self);
        if (self.delegate && [self.delegate respondsToSelector:@selector(clickSyncBlin:AndView:)])
        {
            [self.delegate clickSyncBlin:sender AndView:self];
        }
    } forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.shareScrollView1 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView.mas_top).offset(64);
        make.width.mas_equalTo(self.contentView);
        make.height.mas_equalTo(self.scrollViewHeight);
    }];
    
    [self.splitLine1 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.shareScrollView1.mas_bottom);
        make.width.mas_equalTo(self.contentView);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.shareScrollView2 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.splitLine1.mas_bottom).mas_equalTo(24);
        make.width.mas_equalTo(self.contentView);
        make.height.mas_equalTo(self.scrollViewHeight);
    }];
    
    [self.splitLine2 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.shareScrollView2.mas_bottom);
        make.width.mas_equalTo(self.contentView);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.cancelBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.splitLine2.mas_bottom).offset(24);
        make.width.mas_equalTo(self.contentView);
        make.height.mas_equalTo(self.cancelBtnHeight);
    }];
}

#pragma mark -- 加载分享平台

- (void)setShareInfos:(NSArray<NSArray<CNShareItem *> *> *)shareInfos{
    NSInteger index = 0 ;
    for(NSArray *array in shareInfos){
        NSInteger subIndex = 0 ;
        for(CNShareItem *item in array){
            UIButton *btn = [self createShareBtnWithItem:item];
            if(index == 0){
                [self.shareScrollView1 addSubview:btn];

                [btn setFrame:CGRectMake(subIndex * (self.shareBtnSize.width + self.shareBtnSpace) + self.shareBtnSpace, (self.scrollViewHeight - self.shareBtnSize.height) / 2 , self.shareBtnSize.width, self.shareBtnSize.height)];
            }else{
                [self.shareScrollView2 addSubview:btn];
                [btn setFrame:CGRectMake(subIndex * (self.shareBtnSize.width + self.shareBtnSpace) + self.shareBtnSpace, (self.scrollViewHeight - self.shareBtnSize.height) / 2, self.shareBtnSize.width, self.shareBtnSize.height)];
            }
//            [self setButtonContentCenter:btn];
            
            subIndex ++ ;
        }
        index ++ ;
    }
    self.shareScrollView1.contentSize = CGSizeMake(shareInfos.firstObject.count * (self.shareBtnSize.width + self.shareBtnSpace) + 2*self.shareBtnSpace, 0);
    self.shareScrollView2.contentSize = CGSizeMake(shareInfos.lastObject.count * (self.shareBtnSize.width + self.shareBtnSpace) + 2*self.shareBtnSpace, 0);
}

- (UIButton *)createShareBtnWithItem:(CNShareItem *)item{
    CNCustomButton *btn = [CNCustomButton buttonWithType:UIButtonTypeCustom];
    btn.imagePosition = CNCustomButtonImagePositionTop;
    btn.interTitleImageSpacing = 8;
    [btn setTitle:item.title forState:UIControlStateNormal];
    [btn theme_setImage:item.imagePicker forState:UIControlStateNormal];
    [btn theme_setImage:item.imagePicker forState:UIControlStateHighlighted];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:10]];
    [btn theme_setTitleColor:cellDesTitleTextColorPicker forState:UIControlStateNormal];
    [btn setTag:item.shareType];
    
    [btn addTarget:self action:@selector(shareBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    btn.titleLabel.adjustsFontSizeToFitWidth = YES;;

    return btn ;
}
- (void)updateImagePicker:(ThemeImagePicker *)imagePicker ForShareType:(CNShareType)shareType{
    UIButton *button = (UIButton *)[self viewWithTag:shareType];
    [button theme_setImage:imagePicker forState:UIControlStateNormal];
    [button theme_setImage:imagePicker forState:UIControlStateHighlighted];

}
- (void)updateTitle:(NSString *)title ForShareType:(CNShareType)shareType{
    UIButton *button = (UIButton *)[self viewWithTag:shareType];
    [button setTitle:title forState:UIControlStateNormal];
}
- (void)updateTitleColor:(UIColor *)titleColor ForShareType:(CNShareType)shareType{
    UIButton *button = (UIButton *)[self viewWithTag:shareType];

    [button setTitleColor:titleColor forState:UIControlStateNormal];

}

-(void)setButtonContentCenter:(UIButton *) btn{
////    CGSize imgViewSize,titleSize,btnSize;
////    UIEdgeInsets imageViewEdge,titleEdge;
//    CGFloat heightSpace = 8.0f;
//
//    //设置按钮内边距
////    imgViewSize = CGSizeMake(48, 48);
////    titleSize = btn.titleLabel.bounds.size;
////    btnSize = btn.bounds.size;
////
////    imageViewEdge = UIEdgeInsetsMake(0,0, btnSize.height -imgViewSize.height - heightSpace, - 10);
////    [btn setImageEdgeInsets:imageViewEdge];
////
////    titleEdge = UIEdgeInsetsMake(imgViewSize.height +heightSpace, 0, 0.0, 0.0);
////    [btn setTitleEdgeInsets:titleEdge];
//
//    CGSize imageSize = CGSizeMake(48, 48);
//    
//    [btn setTitleEdgeInsets:UIEdgeInsetsMake(imageSize.height+heightSpace, -imageSize.width, 0, 0.0)];
//
//    [btn setImageEdgeInsets:UIEdgeInsetsMake(-4,0, btn.size.height -48 - heightSpace, - 10)];
    
    CGSize imageSize = btn.imageView.frame.size;
    CGSize titleSize = btn.titleLabel.frame.size;
    CGFloat spacing = 8;
    CGFloat totalHeight = (imageSize.height + titleSize.height + spacing);
    
    btn.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height), 0.0, 0.0, - titleSize.width);
    
    btn.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, - (totalHeight - titleSize.height),0.0);
    
}

- (void)shareBtnClicked:(id)sender{
    UIButton *btn = (UIButton *)sender;
    if(self.delegate && [self.delegate respondsToSelector:@selector(shareWithType:)]){
        [self.delegate shareWithType:btn.tag];
    }
//    if(btn.tag != CNShareTypeDigg){
        [self hideShareView];

//    }
}

#pragma mark -- 分享视图的显示隐藏

- (void)showShareView{
    NSMutableArray *array1 = [NSMutableArray arrayWithCapacity:0];
    [self.shareScrollView1.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if([obj isKindOfClass:[UIButton class]]){
            [array1 addObject:obj];
        }
    }];
    
    NSMutableArray *array2 = [NSMutableArray arrayWithCapacity:0];
    [self.shareScrollView2.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if([obj isKindOfClass:[UIButton class]]){
            [array2 addObject:obj];
        }
    }];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.bgView.alpha = 1.0 ;
        self.contentView.frame = CGRectMake(0, [[UIScreen mainScreen]bounds].size.height - self.contentViewHeight, [[UIScreen mainScreen]bounds].size.width, self.contentViewHeight);
    }];
    
    NSTimeInterval delay = 0.0 ;
    for(UIButton *btn in array1){
        CGAffineTransform tran = CGAffineTransformMakeTranslation(0, self.scrollViewHeight);
        btn.transform = tran ;
        delay += 0.08;
        [UIView animateWithDuration:0.7 delay:delay usingSpringWithDamping:0.7 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            btn.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
        }];
    }
    
    delay = 0.0;
    for(UIButton *btn in array2){
        CGAffineTransform tran = CGAffineTransformMakeTranslation(0, self.scrollViewHeight);
        btn.transform = tran ;
        delay += 0.08;
        [UIView animateWithDuration:0.7 delay:delay usingSpringWithDamping:0.7 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            btn.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
        }];
    }

}

- (void)hideShareView{
    [UIView animateWithDuration:0.3 animations:^{
        self.bgView.alpha = 0.0;
        self.contentView.frame = CGRectMake(0, [[UIScreen mainScreen]bounds].size.height, [[UIScreen mainScreen]bounds].size.width, self.contentViewHeight);
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark -- 广告视图点击

- (void)adViewClicked{
    
}

#pragma mark -- @property

- (UIButton *)bgView{
    if(!_bgView){
        _bgView = [[UIButton alloc]init];
        _bgView.backgroundColor = [[UIColor colorWithHexString:@"06060d"]colorWithAlphaComponent:0.4];
        _bgView.alpha = 0.0 ;
        [_bgView addTarget:self action:@selector(hideShareView) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bgView;
}

- (UIView *)contentView{
    if(!_contentView){
        _contentView = [[UIView alloc]init];
        _contentView.theme_backgroundColor = sharePageBGColorPicker;
        _contentView.userInteractionEnabled = YES;
    }
    return _contentView;
}

- (UIView *)splitLine1{
    if(!_splitLine1){
        _splitLine1 = [[UIView alloc]init];
        _splitLine1.theme_backgroundColor = sharePageBGColorPicker;
    }
    return _splitLine1;
}

- (UIScrollView *)shareScrollView1{
    if(!_shareScrollView1){
        _shareScrollView1 = [[UIScrollView alloc]init];
        _shareScrollView1.theme_backgroundColor = sharePageBGColorPicker;
        _shareScrollView1.showsVerticalScrollIndicator = NO ;
        _shareScrollView1.showsHorizontalScrollIndicator = NO ;
        _shareScrollView1.scrollEnabled = YES ;
        _shareScrollView1.clipsToBounds = YES ;
        _shareScrollView1.bounces = YES ;
    }
    return _shareScrollView1;
}

- (UIView *)splitLine2{
    if(!_splitLine2){
        _splitLine2 = [[UIView alloc]init];
        _splitLine2.theme_backgroundColor = sharePageBGColorPicker;

        //_splitLine2.backgroundColor = [[UIColor grayColor]colorWithAlphaComponent:0.2];
    }
    return _splitLine2;
}

- (UIScrollView *)shareScrollView2{
    if(!_shareScrollView2){
        _shareScrollView2 = [[UIScrollView alloc]init];
        _shareScrollView2.theme_backgroundColor = sharePageBGColorPicker;
        _shareScrollView2.showsVerticalScrollIndicator = NO ;
        _shareScrollView2.showsHorizontalScrollIndicator = NO ;
        _shareScrollView2.clipsToBounds = YES ;
        _shareScrollView2.scrollEnabled = YES ;
        _shareScrollView2.bounces = YES ;
    }
    return _shareScrollView2;
}

- (UIButton *)cancelBtn{
    if(!_cancelBtn){
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn theme_setTitleColor:cellTitleTextColorPicker forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font = [UIFont fontWithName:@"SFProText-Regular" size:15];
        [_cancelBtn addTarget:self action:@selector(hideShareView) forControlEvents:UIControlEventTouchUpInside];
        _cancelBtn.theme_backgroundColor = menuPageBGColorPicker;
    }
    return _cancelBtn;
}

@end
